// @ts-ignore
/* eslint-disable */
import { request } from '@umijs/max';

const API_BASE_URL = '/api';

// 获取合同列表
export async function fetchContractList(params) {
  try {
    const response = await request(`${API_BASE_URL}/contracts`, {
      method: 'POST',
      data: params,
    });
    return { data: response.data.records, success: true, total: response.data.total };
  } catch (error) {
    throw new Error('Failed to fetch contract list');
  }
}

// 删除合同
export async function deleteContract(id) {
  try {
    await request(`${API_BASE_URL}/contracts/${id}`, {
      method: 'GET',
    });
  } catch (error) {
    throw new Error('Failed to delete contract');
  }
}

// 更新合同
export async function updateContract(contract) {
  try {
    await request(`${API_BASE_URL}/contracts/${contract.id}`, {
      method: 'POST',
      data: contract,
    });
  } catch (error) {
    throw new Error('Failed to update contract');
  }
}

// 下载合同附件
export async function downloadAttachment(id) {
  try {
    const response = await request(`${API_BASE_URL}/contracts/${id}/attachment`, {
      method: 'GET',
      responseType: 'blob',
    });
    const url = window.URL.createObjectURL(new Blob([response]));
    const link = document.createElement('a');
    link.href = url;
    link.setAttribute('download', `contract_attachment_${id}.pdf`);
    document.body.appendChild(link);
    link.click();
  } catch (error) {
    throw new Error('Failed to download attachment');
  }
}